package contegor.scire.core.queries;

import contegor.scire.core.Connectable;
import contegor.scire.core.Version;

/**
 * Logic 'and' {@link Condition} of the ones given to the constructor. They are evaluated
 * from left to right and when one is false, evaluation stops and <code>false</code> is returned. 
 *
 * @version $Revision: 1.3 $
 * @author  Héctor Cirbián Pousa ($Author: hector $)
 */
@Version("$Id: And.java,v 1.3 2010-02-01 10:55:41 hector Exp $")
public class And<C extends Connectable> extends Condition<C> {
  private static final long serialVersionUID = -3082811881018602964L;

  private Condition<C>[] conditions;

  public And(Condition<C> ... conditions) {
    super();
    this.conditions = conditions;
  }

  /* (non-Javadoc)
   * @see contegor.scire.core.queries.Condition#eval(contegor.scire.core.Connectable)
   */
  @Override
  public boolean eval(C connectable) {
    for (Condition<C> condition: conditions)
      if (!condition.eval(connectable))
        return false;
    return true;
  }

  @Override
  @SuppressWarnings("unchecked")
  public And<C> clone() {
    Condition<C>[] aux = new Condition[this.conditions.length];
    for (int i=0; i<this.conditions.length; i++)
      aux[i] = this.conditions[i].clone();
    return new And<C>(aux);
  }
}
